package org.shagin.jfin.sheet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class SheetRow {

	private HashMap<SheetColumn<?>, SheetCell> rowData = new HashMap<SheetColumn<?>, SheetCell>();

	public SheetRow() {
	}

	public void setCell(SheetColumn<?> col, SheetCell cell) {
		if (cell == null)
			rowData.remove(col);
		else
			rowData.put(col, cell);
	}

	public SheetCell getCell(SheetColumn<?> col) {
		return rowData.get(col);
	}

	public Number getNumber(SheetColumn<?> col, Number defValue) {
		Number result = defValue;
		SheetCell cell = getCell(col);
		if (cell != null)
			result = cell.getNumber();
		return result;
	}

	public List<SheetCell> getCells() {
		List<SheetCell> result = new ArrayList<SheetCell>();
		for (Map.Entry<SheetColumn<?>, SheetCell> entry : rowData.entrySet())
			result.add(entry.getValue());
		return result;
	}

	public Double getDouble(SheetColumn<?> col) {
		return getNumber(col, 0d).doubleValue();
	}

	public void setDouble(SheetColumn<?> col, double value) {
		setCell(col, new SheetCell(new Double(value)));
	}

	public String getString(SheetColumn<?> col, String defValue) {
		String result = defValue;
		SheetCell cell = getCell(col);
		if (cell != null && cell.getObject() != null)
			result = cell.getObject().toString();
		return result;
	}

	public void setString(SheetColumn<?> col, String value) {
		setCell(col, new SheetCell(value));
	}
}
